#课程练习
#1-定义（创建函数）
def printMyAddress():
    print("Warren Sande")
    print("123 Main Street")
    print("Ottawa, Ontario, Canada")
    print("K2M 2E9")
    print()

#2-使用函数
printMyAddress()


#向函数传递参数
print("\n向函数传递一个参数")
def printMyAddress(myName):
    print(myName)
    print("123 Main Street")
    print("Ottawa, Ontario, Canada")
    print("K2M 2E9")
    print()

printMyAddress('咖啡')
printMyAddress("张三")
printMyAddress("李四")

#向函数传递参数
print("\n向函数传递多个参数")
def printMyAddress(myName,houseNum):
    print(myName)
    print(houseNum)
    print("Ottawa, Ontario, Canada")
    print("K2M 2E9")
    print()

printMyAddress('Carter Sande','45')
printMyAddress("Jack Black",'64')
printMyAddress("Tom Green",'22')
printMyAddress("Todd White",'36')


#返回信息
print("函数返回值")
def calculateTax(price,tax_rate):
    taxTotal=price+(price*tax_rate)
    return taxTotal

totalPrice=calculateTax(7.99,0.06)
print(totalPrice)

total=calculateTax(7.99,0.06)+calculateTax(6.59,0.08)
print(total)

my_price=float(input("请输入价格："))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price," Total price= ",totalPrice)

#变量的作用域
print("变量作用域")
def calculateTax(price,tax_rate):
    taxTotal=price+(price*tax_rate)
    my_testPrice=10000
    print("my_testPrice(函数内)=",my_testPrice)
    return taxTotal

my_testPrice=float(input("请输入价格："))
totalPrice=calculateTax(my_testPrice,0.06)
print("my_testPrice(函数外)=",my_testPrice," Total price= ",totalPrice)


print("变量作用域2")
def calculateTax(price,tax_rate):
    taxTotal=price+(price*tax_rate)
    #强制在函数内使用全局版的变量[my_testPrice2]
    global my_testPrice2
    my_testPrice2=10000
    print("my_testPrice(函数内)=",my_testPrice2)
    return taxTotal

my_testPrice2=float(input("请输入价格："))
totalPrice=calculateTax(my_testPrice2,0.06)
print("my_testPrice(函数外)=",my_testPrice2," Total price= ",totalPrice)